package com.jyw.business.ast.domain.dto.web;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.jyw.business.ast.constant.AstInfoEquipmentStateConstant;
import com.jyw.business.ast.domain.AstConsumable;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.system.service.ISysDictDataService;
import com.jyw.system.utils.DeptUtils;
import com.jyw.system.utils.UserUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;

@Data
@ApiModel("资产导入请求实体")
@Slf4j
public class AstInfoImportDto {

    @ApiModelProperty("资产名称")
    @ExcelProperty(index = 0)
    private String name;

    @ApiModelProperty("资产类型")
    @ExcelProperty(index = 1)
    private String assetsType;

    @ApiModelProperty("资产来源")
    @ExcelProperty(index = 2)
    private String assetsSource;

    @ApiModelProperty("资产规格型号")
    @ExcelProperty(index = 3)
    private String model;

    @ApiModelProperty("所属巡检单元")
    @ExcelProperty(index = 4)
    private String pertainUnit;

    @ApiModelProperty("使用科室")
    @ExcelProperty(index = 5)
    private String useDept;

    @ApiModelProperty("使用责任人")
    @ExcelProperty(index = 6)
    private String personLiable;

    @ApiModelProperty("所在位置")
    @ExcelProperty(index = 7)
    private String place;

    @ApiModelProperty("备注")
    @ExcelProperty(index = 8)
    private String remark;

    @ApiModelProperty("资产编号")
    @ExcelProperty(index = 9)
    private String assetsCode;

    @ApiModelProperty("设备S/N码")
    @ExcelProperty(index = 10)
    private String seriesNumb;

    @ApiModelProperty("使用日期")
    @ExcelProperty(index = 11)
    @DateTimeFormat("yyyy/MM/dd")
    private Date useDate;

    @ApiModelProperty("使用年限")
    @ExcelProperty(index = 12)
    private Float years;

    @ApiModelProperty("保养维护")
    @ExcelProperty(index = 13)
    private Integer meteringTimer;

    @ApiModelProperty("维护方")
    @ExcelProperty(index = 14)
    private String maintainObj;

    @ApiModelProperty("维保公司")
    @ExcelProperty(index = 15)
    private String maintainCompany;

    @ApiModelProperty("维保责任人")
    @ExcelProperty(index = 16)
    private String maintainUser;

    @ApiModelProperty("维保到期日期")
    @ExcelProperty(index = 17)
    private Date maintainTerm;

    @ApiModelProperty("采购日期")
    @ExcelProperty(index = 18)
    private Date purchaseDate;

    @ApiModelProperty("采购编号")
    @ExcelProperty(index = 19)
    private String purchaseCode;

    @ApiModelProperty("采购价格")
    @ExcelProperty(index = 20)
    private String purchasePrice;

    @ApiModelProperty("采购数量")
    @ExcelProperty(index = 21)
    private Long purchaseNumber;

    @ApiModelProperty("所属项目")
    @ExcelProperty(index = 22)
    private String projectName;

    @ApiModelProperty("品牌")
    @ExcelProperty(index = 23)
    private String brand;

    @ApiModelProperty("生产厂家")
    @ExcelProperty(index = 24)
    private String manufacturer;

    @ApiModelProperty("产地")
    @ExcelProperty(index = 25)
    private String originPlace;

    @ApiModelProperty("售后电话")
    @ExcelProperty(index = 26)
    private String salesPhone;

    @ApiModelProperty("供应商")
    @ExcelProperty(index = 27)
    private String supplier;

    @ApiModelProperty("供应商电话")
    @ExcelProperty(index = 28)
    private String supplierPhone;

    @ApiModelProperty("IP地址")
    @ExcelProperty(index = 29)
    private String ipPlace;

    @ApiModelProperty("MAC地址")
    @ExcelProperty(index = 30)
    private String macPlace;

    @ApiModelProperty("操作系统")
    @ExcelProperty(index = 31)
    private String operatingSystem;

    @ApiModelProperty("系统版本")
    @ExcelProperty(index = 32)
    private String systemVersion;

    @ApiModelProperty("配置信息")
    @ExcelProperty(index = 33)
    private String configuration;

    public AstInfo buildInfo(){
        ISysDictDataService dataService = SpringUtils.getBean(ISysDictDataService.class);
        IAstInfoService infoService = SpringUtils.getBean(IAstInfoService.class);

        AstInfo info = new AstInfo();
        BeanUtils.copyProperties(this,info);
        if(!ObjectUtils.isEmpty(getYears())){
            info.setYears(BigDecimal.valueOf(getYears()));
        }
        info.setUseUnit("1");
        info.setEquipmentState(AstInfoEquipmentStateConstant.NORMAL);
        info.setAssetsType(dataService.getDictValueSave(SysDictTypeConstant.AST_ASSET_TYPE,getAssetsType()));
        info.setAssetsSource(dataService.getDictValue(SysDictTypeConstant.AST_ASSET_SOURCE,getAssetsSource()));
        if(ObjectUtils.isEmpty(getAssetsCode())){
            info.setAssetsCode(infoService.generateAssetCode());
        }
        info.setUseDept(DeptUtils.nameToId(getUseDept()));
        if(ObjectUtils.isEmpty(info.getUseDept())){
            info.setIsPublic(1);
        }else{
            info.setIsPublic(0);
        }
        info.setMeteringUnit("1");
        info.setPersonLiable(UserUtils.nameToId(getPersonLiable()));
        info.setMaintainCompany(DeptUtils.nameToId(getMaintainCompany()));
        info.setMaintainUser(UserUtils.nameToId(getMaintainUser()));
        info.setMaintainInsideType(3);
        info.setMaintainObjId(DeptUtils.nameToId(getMaintainObj()));
        info.setPurchasePrice(converBigDecimal(getPurchasePrice()));
        info.setBrand(dataService.getDictValueSave(SysDictTypeConstant.AST_ASSET_BRAND,getBrand()));
        info.setOperatingSystem(dataService.getDictValueSave(SysDictTypeConstant.AST_ASSET_OPERATING_SYSTEM,getOperatingSystem()));
        info.setState(1);
        info.setCreateUser(SecurityUtils.getUserId());
        info.setCreateTime(DateUtils.getNowDate());
        if(!ObjectUtils.isEmpty(info.getUseDept())&&!ObjectUtils.isEmpty(info.getYears())&&!ObjectUtils.isEmpty(info.getUseUnit())){
            info.setUseOverDate(DateUtils.calcAstDate(info.getUseDate(),info.getYears(),info.getUseUnit()));
        }
        if(!ObjectUtils.isEmpty(info.getUseDate())&&!ObjectUtils.isEmpty(info.getMeteringTimer())&&!ObjectUtils.isEmpty(info.getMeteringUnit())){
            info.setNextMeteringDate(DateUtils.calcAstDate(info.getUseDate(),BigDecimal.valueOf(info.getMeteringTimer()),info.getMeteringUnit()));
        }
        info.setPertainUnit(infoService.findInsUnitByName(getPertainUnit()));
        return info;
    };

    private BigDecimal converBigDecimal(String str){
        if(!ObjectUtils.isEmpty(str)){
            try {
                BigDecimal bigDecimal = new BigDecimal(str);
                return bigDecimal;
            }catch (Exception e){

            }
        }
        return null;
    }
}
